  format mz
  include "../common/pminc.asm"
  include "../common/libx86.asm"


  .GDT
  DUMMY_DESC
  normal  DESC  0,      0xffff,     SDT_DATA_RW
  vedio   DESC  0x200000,0x3000-1,  SDT_DATA_RW
  .block g0init
  .endb

  
  .data dat

  serr_cpuid db "CPUID is not supported by this CPU!",0x0d,0x0a,'$'
  serr_pae db "PAE is not supported by this CPU!",0x0d,0x0a,'$'
  serr_lmode db "x86-64 Long Mode is not supported by this CPU!",0x0d,0x0a,'$'
  serr_vmx db "VMX is not supported by this CPU!",0x0d,0x0a,'$'

  sok_pae db "PAE FEATURE is supported!",0x0d,0x0a,'$'
  sok_lmode db "LONG-MODE FEATURE is supported!",0x0d,0x0a,'$'
  sok_vmx db "VMX Feature is supported!",0x0d,0x0a,'$'

  VgdtR XDTR GDTSEG.limit, 0
  orgSP dw 0
  orgSS dw 0



  .code main
start:
  mov ax,dat
  mov ds,ax
  IsCpuSupportCPUID
  test eax,eax
  jz exit_prog.err_cpuid
  IsCpuSupportPAE
  test eax,eax
  jz exit_prog.err_pae

  mov dx,sok_pae
  iDos21 9

  IsCpuSupportLmode
  test eax,eax
  jnz continue_prog
warns:
  mov dx,serr_lmode
  iDos21 9

continue_prog:
  mov dx,sok_lmode
  iDos21 9
  
  IsCpuSupportVMX
  test eax,eax
  jz exit_prog.err_vmx
  
  mov dx,sok_vmx
  iDos21 9


if 0
enter_pmode:
  mov eax,GDTSEG
  shl eax,4
  shld edx,eax,16      ;seg*16
  mov [VgdtR.base_lo],ax
  mov [VgdtR.base_hi],dx  ;init GDTR

  ;init GDT descriptors
  mov ax,GDTSEG
  mov ds,ax
  mov si,g0init
  mov cx,g0init.items
  call ..init_xdt
  
  mov ax,dat
  mov ds,ax
  mov [orgSP],sp
  mov [orgSS],ss

  cli        
  EnableA20 
  lgdt fword [VgdtR]
  SetCr0Pebit
  ;jmp fword gc32.sel:at_pmode32

exit_pmode:

  mov ax,normal.sel
  mov ds,ax
  mov es,ax
  mov ss,ax
  mov fs,ax
  mov gs,ax
  cli
	ClrCr0Pebit
  jmp dword main:back_at_real_mode

back_at_real_mode:
  mov ax,dat
  mov ds,ax
  lss sp,dword [orgSP]
  DisableA20
  sti
end if

exit_prog:
  mov ax,0x4c00
  int 21h

.err_cpuid:
  mov dx,serr_cpuid
  iDos21	9
  jmp exit_prog

.err_pae:
  mov dx,serr_pae
  iDos21	9
  jmp exit_prog

.err_vmx:
  mov dx,serr_vmx
  iDos21	9
  jmp exit_prog

  init_xdt_proc
.end main:start